library(foreign)
electionDat <- read.dta("/users/brucedesmarais/Dropbox/professional/Research/Submitted/Camp/Data/matchingReplication.dta")

quintile <- function(x,slices=16){
	quints <- quantile(x,prob=seq(0,1,length=slices+2),na.rm=T)[-c(1,slices+2)]
	qtile <- 1
	for(i in 1:length(quints)){
		qtile <- qtile + (x>quints[i])
	}
	qtile
}

matchVec <- c("Cexp",  "Iexp","Cqual","year","Rinc","Dinc","Ivs")

vec2form <- function(x){
	form <- x[1]
	form <- paste(form,"~",paste(x[-1],collapse="+"),sep="")
	as.formula(form)
}

ssize <- NULL

EffectsList <- list()

for(ex in 1:length(matchVec)){
matchVecEx <- matchVec[-ex]
matchForm <- vec2form(c("CNtriv",matchVecEx))	

###### ATE #######
smallest <- 0
slc1 <- 5
slc2 <- 6
slc3 <- 6
while(smallest < .1){

slc1 = slc1 + 2

library(Matching)
CNtriv <- electionDat$DBClust*(electionDat$inc==0)+electionDat$RBClust*(electionDat$inc==1)
Cwin = (electionDat$pwin==1)*(electionDat$inc==0)+(electionDat$pwin==0)*(electionDat$inc==1)
Cexp <- electionDat$dexp*(electionDat$inc==0)+electionDat$rexp*(electionDat$inc==1)
Iexp <- electionDat$dexp*(electionDat$inc==1)+electionDat$rexp*(electionDat$inc==0)
#Iexp <- round(Iexp/(exp(1-slc1/3)*sd(Iexp,na.rm=T)))
Iexp <- quintile(Iexp,slices=slc1)
#Cexp <- round(Cexp/(exp(1-slc2/3)*sd(Cexp,na.rm=T)))
Cexp <- quintile(Cexp, slices=slc2)
Cvs <- electionDat$dv*(electionDat$inc==0)+(100-electionDat$dv)*(electionDat$inc==1)
Ivs <- electionDat$dvp*(electionDat$inc==1)+(100-electionDat$dvp)*(electionDat$inc==0)
#Ivs <- round(Ivs/(exp(1-slc3/3)*sd(Ivs,na.rm=T)))
Ivs <- quintile(Ivs, slices=slc3)
dat <- na.omit(data.frame(CNtriv,Cvs,Cwin,Cexp,Iexp,Cqual = as.numeric(electionDat$po1==1),year=electionDat$year,Rinc=as.numeric(electionDat$inc==0),Dinc = as.numeric(electionDat$inc==1),Ivs,yrStDist=electionDat$yrStDist))

ATTwin <- Match(dat$Cwin,Tr=dat$CNtriv,X=dat[,matchVecEx],exact=T,estimand="ATT")
ATEwin <- Match(dat$Cwin,Tr=dat$CNtriv,X=dat[,matchVecEx],exact=T,estimand="ATE")
ATCwin <- Match(dat$Cwin,Tr=dat$CNtriv,X=dat[,matchVecEx],exact=T,estimand="ATC")

### Checking Balance
CNtriv <- electionDat$DBClust*(electionDat$inc==0)+electionDat$RBClust*(electionDat$inc==1)
Cwin = (electionDat$pwin==1)*(electionDat$inc==0)+(electionDat$pwin==0)*(electionDat$inc==1)
Cexp <- electionDat$dexp*(electionDat$inc==0)+electionDat$rexp*(electionDat$inc==1)
Iexp <- electionDat$dexp*(electionDat$inc==1)+electionDat$rexp*(electionDat$inc==0)
#Iexp <- round(Iexp/(.5*sd(Iexp,na.rm=T)))
#Iexp <- quintile(Iexp)
#Cexp <- round(Cexp/(.5*sd(Cexp,na.rm=T)))
#Cexp <- quintile(Cexp)
Cvs <- electionDat$dv*(electionDat$inc==0)+(100-electionDat$dv)*(electionDat$inc==1)
Ivs <- electionDat$dvp*(electionDat$inc==1)+(100-electionDat$dvp)*(electionDat$inc==0)
#Ivs <- round(Ivs/(.5*sd(Ivs,na.rm=T)))
#Ivs <- quintile(Ivs)
dat <- na.omit(data.frame(CNtriv,Cvs,Cwin,Cexp,Iexp,Cqual = as.numeric(electionDat$po1==1),year=electionDat$year,Rinc=as.numeric(electionDat$inc==0),Dinc = as.numeric(electionDat$inc==1),Ivs))

balchk <- MatchBalance(matchForm,match.out=ATEwin,data=dat)

smallest <- balchk$AMsmallest.p.value
 

if(smallest < .1){

slc2 <- slc2 +2


library(Matching)
CNtriv <- electionDat$DBClust*(electionDat$inc==0)+electionDat$RBClust*(electionDat$inc==1)
Cwin = (electionDat$pwin==1)*(electionDat$inc==0)+(electionDat$pwin==0)*(electionDat$inc==1)
Cexp <- electionDat$dexp*(electionDat$inc==0)+electionDat$rexp*(electionDat$inc==1)
Iexp <- electionDat$dexp*(electionDat$inc==1)+electionDat$rexp*(electionDat$inc==0)
#Iexp <- round(Iexp/(exp(1-slc1/3)*sd(Iexp,na.rm=T)))
Iexp <- quintile(Iexp,slices=slc1)
#Cexp <- round(Cexp/(exp(1-slc2/3)*sd(Cexp,na.rm=T)))
Cexp <- quintile(Cexp, slices=slc2)
Cvs <- electionDat$dv*(electionDat$inc==0)+(100-electionDat$dv)*(electionDat$inc==1)
Ivs <- electionDat$dvp*(electionDat$inc==1)+(100-electionDat$dvp)*(electionDat$inc==0)
#Ivs <- round(Ivs/(exp(1-slc3/3)*sd(Ivs,na.rm=T)))
Ivs <- quintile(Ivs, slices=slc3)
dat <- na.omit(data.frame(CNtriv,Cvs,Cwin,Cexp,Iexp,Cqual = as.numeric(electionDat$po1==1),year=electionDat$year,Rinc=as.numeric(electionDat$inc==0),Dinc = as.numeric(electionDat$inc==1),Ivs,yrStDist=electionDat$yrStDist))



ATTwin <- Match(dat$Cwin,Tr=dat$CNtriv,X=dat[,matchVecEx],exact=T,estimand="ATT")
ATEwin <- Match(dat$Cwin,Tr=dat$CNtriv,X=dat[,matchVecEx],exact=T,estimand="ATE")
ATCwin <- Match(dat$Cwin,Tr=dat$CNtriv,X=dat[,matchVecEx],exact=T,estimand="ATC")

### Checking Balance

CNtriv <- electionDat$DBClust*(electionDat$inc==0)+electionDat$RBClust*(electionDat$inc==1)
Cwin = (electionDat$pwin==1)*(electionDat$inc==0)+(electionDat$pwin==0)*(electionDat$inc==1)
Cexp <- electionDat$dexp*(electionDat$inc==0)+electionDat$rexp*(electionDat$inc==1)
Iexp <- electionDat$dexp*(electionDat$inc==1)+electionDat$rexp*(electionDat$inc==0)
#Iexp <- round(Iexp/(.5*sd(Iexp,na.rm=T)))
#Iexp <- quintile(Iexp)
#Cexp <- round(Cexp/(.5*sd(Cexp,na.rm=T)))
#Cexp <- quintile(Cexp)
Cvs <- electionDat$dv*(electionDat$inc==0)+(100-electionDat$dv)*(electionDat$inc==1)
Ivs <- electionDat$dvp*(electionDat$inc==1)+(100-electionDat$dvp)*(electionDat$inc==0)
#Ivs <- round(Ivs/(.5*sd(Ivs,na.rm=T)))
#Ivs <- quintile(Ivs)
dat <- na.omit(data.frame(CNtriv,Cvs,Cwin,Cexp,Iexp,Cqual = as.numeric(electionDat$po1==1),year=electionDat$year,Rinc=as.numeric(electionDat$inc==0),Dinc = as.numeric(electionDat$inc==1),Ivs))

balchk <- MatchBalance(matchForm,match.out=ATEwin,data=dat)

smallest <- balchk$AMsmallest.p.value

	
}

if(smallest < .1){

slc3 <- slc3 +2


library(Matching)
CNtriv <- electionDat$DBClust*(electionDat$inc==0)+electionDat$RBClust*(electionDat$inc==1)
Cwin = (electionDat$pwin==1)*(electionDat$inc==0)+(electionDat$pwin==0)*(electionDat$inc==1)
Cexp <- electionDat$dexp*(electionDat$inc==0)+electionDat$rexp*(electionDat$inc==1)
Iexp <- electionDat$dexp*(electionDat$inc==1)+electionDat$rexp*(electionDat$inc==0)
#Iexp <- round(Iexp/(exp(1-slc1/3)*sd(Iexp,na.rm=T)))
Iexp <- quintile(Iexp,slices=slc1)
#Cexp <- round(Cexp/(exp(1-slc2/3)*sd(Cexp,na.rm=T)))
Cexp <- quintile(Cexp, slices=slc2)
Cvs <- electionDat$dv*(electionDat$inc==0)+(100-electionDat$dv)*(electionDat$inc==1)
Ivs <- electionDat$dvp*(electionDat$inc==1)+(100-electionDat$dvp)*(electionDat$inc==0)
#Ivs <- round(Ivs/(exp(1-slc3/3)*sd(Ivs,na.rm=T)))
Ivs <- quintile(Ivs, slices=slc3)
dat <- na.omit(data.frame(CNtriv,Cvs,Cwin,Cexp,Iexp,Cqual = as.numeric(electionDat$po1==1),year=electionDat$year,Rinc=as.numeric(electionDat$inc==0),Dinc = as.numeric(electionDat$inc==1),Ivs,yrStDist=electionDat$yrStDist))

ATTwin <- Match(dat$Cwin,Tr=dat$CNtriv,X=dat[,matchVecEx],exact=T,estimand="ATT")
ATEwin <- Match(dat$Cwin,Tr=dat$CNtriv,X=dat[,matchVecEx],exact=T,estimand="ATE")
ATCwin <- Match(dat$Cwin,Tr=dat$CNtriv,X=dat[,matchVecEx],exact=T,estimand="ATC")

### Checking Balance

CNtriv <- electionDat$DBClust*(electionDat$inc==0)+electionDat$RBClust*(electionDat$inc==1)
Cwin = (electionDat$pwin==1)*(electionDat$inc==0)+(electionDat$pwin==0)*(electionDat$inc==1)
Cexp <- electionDat$dexp*(electionDat$inc==0)+electionDat$rexp*(electionDat$inc==1)
Iexp <- electionDat$dexp*(electionDat$inc==1)+electionDat$rexp*(electionDat$inc==0)
#Iexp <- round(Iexp/(.5*sd(Iexp,na.rm=T)))
#Iexp <- quintile(Iexp)
#Cexp <- round(Cexp/(.5*sd(Cexp,na.rm=T)))
#Cexp <- quintile(Cexp)
Cvs <- electionDat$dv*(electionDat$inc==0)+(100-electionDat$dv)*(electionDat$inc==1)
Ivs <- electionDat$dvp*(electionDat$inc==1)+(100-electionDat$dvp)*(electionDat$inc==0)
#Ivs <- round(Ivs/(.5*sd(Ivs,na.rm=T)))
#Ivs <- quintile(Ivs)
dat <- na.omit(data.frame(CNtriv,Cvs,Cwin,Cexp,Iexp,Cqual = as.numeric(electionDat$po1==1),year=electionDat$year,Rinc=as.numeric(electionDat$inc==0),Dinc = as.numeric(electionDat$inc==1),Ivs))

balchk <- MatchBalance(matchForm,match.out=ATEwin,data=dat)

smallest <- balchk$AMsmallest.p.value

	
}



}

ATE <- ATEwin



###### ATT #######
smallest <- 0
slc1 <- 5
slc2 <- 6
slc3 <- 6
while(smallest < .1){

slc1 = slc1 + 2

library(Matching)
CNtriv <- electionDat$DBClust*(electionDat$inc==0)+electionDat$RBClust*(electionDat$inc==1)
Cwin = (electionDat$pwin==1)*(electionDat$inc==0)+(electionDat$pwin==0)*(electionDat$inc==1)
Cexp <- electionDat$dexp*(electionDat$inc==0)+electionDat$rexp*(electionDat$inc==1)
Iexp <- electionDat$dexp*(electionDat$inc==1)+electionDat$rexp*(electionDat$inc==0)
#Iexp <- round(Iexp/(exp(1-slc1/3)*sd(Iexp,na.rm=T)))
Iexp <- quintile(Iexp,slices=slc1)
#Cexp <- round(Cexp/(exp(1-slc2/3)*sd(Cexp,na.rm=T)))
Cexp <- quintile(Cexp, slices=slc2)
Cvs <- electionDat$dv*(electionDat$inc==0)+(100-electionDat$dv)*(electionDat$inc==1)
Ivs <- electionDat$dvp*(electionDat$inc==1)+(100-electionDat$dvp)*(electionDat$inc==0)
#Ivs <- round(Ivs/(exp(1-slc3/3)*sd(Ivs,na.rm=T)))
Ivs <- quintile(Ivs, slices=slc3)
dat <- na.omit(data.frame(CNtriv,Cvs,Cwin,Cexp,Iexp,Cqual = as.numeric(electionDat$po1==1),year=electionDat$year,Rinc=as.numeric(electionDat$inc==0),Dinc = as.numeric(electionDat$inc==1),Ivs,yrStDist=electionDat$yrStDist))

ATTwin <- Match(dat$Cwin,Tr=dat$CNtriv,X=dat[,matchVecEx],exact=T,estimand="ATT")
ATEwin <- Match(dat$Cwin,Tr=dat$CNtriv,X=dat[,matchVecEx],exact=T,estimand="ATE")
ATCwin <- Match(dat$Cwin,Tr=dat$CNtriv,X=dat[,matchVecEx],exact=T,estimand="ATC")

### Checking Balance

CNtriv <- electionDat$DBClust*(electionDat$inc==0)+electionDat$RBClust*(electionDat$inc==1)
Cwin = (electionDat$pwin==1)*(electionDat$inc==0)+(electionDat$pwin==0)*(electionDat$inc==1)
Cexp <- electionDat$dexp*(electionDat$inc==0)+electionDat$rexp*(electionDat$inc==1)
Iexp <- electionDat$dexp*(electionDat$inc==1)+electionDat$rexp*(electionDat$inc==0)
#Iexp <- round(Iexp/(.5*sd(Iexp,na.rm=T)))
#Iexp <- quintile(Iexp)
#Cexp <- round(Cexp/(.5*sd(Cexp,na.rm=T)))
#Cexp <- quintile(Cexp)
Cvs <- electionDat$dv*(electionDat$inc==0)+(100-electionDat$dv)*(electionDat$inc==1)
Ivs <- electionDat$dvp*(electionDat$inc==1)+(100-electionDat$dvp)*(electionDat$inc==0)
#Ivs <- round(Ivs/(.5*sd(Ivs,na.rm=T)))
#Ivs <- quintile(Ivs)
dat <- na.omit(data.frame(CNtriv,Cvs,Cwin,Cexp,Iexp,Cqual = as.numeric(electionDat$po1==1),year=electionDat$year,Rinc=as.numeric(electionDat$inc==0),Dinc = as.numeric(electionDat$inc==1),Ivs))

balchk <- MatchBalance(matchForm,match.out=ATTwin,data=dat)

smallest <- balchk$AMsmallest.p.value
 

if(smallest < .1){

slc2 <- slc2 +2


library(Matching)
CNtriv <- electionDat$DBClust*(electionDat$inc==0)+electionDat$RBClust*(electionDat$inc==1)
Cwin = (electionDat$pwin==1)*(electionDat$inc==0)+(electionDat$pwin==0)*(electionDat$inc==1)
Cexp <- electionDat$dexp*(electionDat$inc==0)+electionDat$rexp*(electionDat$inc==1)
Iexp <- electionDat$dexp*(electionDat$inc==1)+electionDat$rexp*(electionDat$inc==0)
#Iexp <- round(Iexp/(exp(1-slc1/3)*sd(Iexp,na.rm=T)))
Iexp <- quintile(Iexp,slices=slc1)
#Cexp <- round(Cexp/(exp(1-slc2/3)*sd(Cexp,na.rm=T)))
Cexp <- quintile(Cexp, slices=slc2)
Cvs <- electionDat$dv*(electionDat$inc==0)+(100-electionDat$dv)*(electionDat$inc==1)
Ivs <- electionDat$dvp*(electionDat$inc==1)+(100-electionDat$dvp)*(electionDat$inc==0)
#Ivs <- round(Ivs/(exp(1-slc3/3)*sd(Ivs,na.rm=T)))
Ivs <- quintile(Ivs, slices=slc3)
dat <- na.omit(data.frame(CNtriv,Cvs,Cwin,Cexp,Iexp,Cqual = as.numeric(electionDat$po1==1),year=electionDat$year,Rinc=as.numeric(electionDat$inc==0),Dinc = as.numeric(electionDat$inc==1),Ivs,yrStDist=electionDat$yrStDist))

ATTwin <- Match(dat$Cwin,Tr=dat$CNtriv,X=dat[,matchVecEx],exact=T,estimand="ATT")
ATEwin <- Match(dat$Cwin,Tr=dat$CNtriv,X=dat[,matchVecEx],exact=T,estimand="ATE")
ATCwin <- Match(dat$Cwin,Tr=dat$CNtriv,X=dat[,matchVecEx],exact=T,estimand="ATC")

### Checking Balance

CNtriv <- electionDat$DBClust*(electionDat$inc==0)+electionDat$RBClust*(electionDat$inc==1)
Cwin = (electionDat$pwin==1)*(electionDat$inc==0)+(electionDat$pwin==0)*(electionDat$inc==1)
Cexp <- electionDat$dexp*(electionDat$inc==0)+electionDat$rexp*(electionDat$inc==1)
Iexp <- electionDat$dexp*(electionDat$inc==1)+electionDat$rexp*(electionDat$inc==0)
#Iexp <- round(Iexp/(.5*sd(Iexp,na.rm=T)))
#Iexp <- quintile(Iexp)
#Cexp <- round(Cexp/(.5*sd(Cexp,na.rm=T)))
#Cexp <- quintile(Cexp)
Cvs <- electionDat$dv*(electionDat$inc==0)+(100-electionDat$dv)*(electionDat$inc==1)
Ivs <- electionDat$dvp*(electionDat$inc==1)+(100-electionDat$dvp)*(electionDat$inc==0)
#Ivs <- round(Ivs/(.5*sd(Ivs,na.rm=T)))
#Ivs <- quintile(Ivs)
dat <- na.omit(data.frame(CNtriv,Cvs,Cwin,Cexp,Iexp,Cqual = as.numeric(electionDat$po1==1),year=electionDat$year,Rinc=as.numeric(electionDat$inc==0),Dinc = as.numeric(electionDat$inc==1),Ivs))

balchk <- MatchBalance(matchForm,match.out=ATTwin,data=dat)

smallest <- balchk$AMsmallest.p.value

	
}

if(smallest < .1){

slc3 <- slc3 +2


library(Matching)
CNtriv <- electionDat$DBClust*(electionDat$inc==0)+electionDat$RBClust*(electionDat$inc==1)
Cwin = (electionDat$pwin==1)*(electionDat$inc==0)+(electionDat$pwin==0)*(electionDat$inc==1)
Cexp <- electionDat$dexp*(electionDat$inc==0)+electionDat$rexp*(electionDat$inc==1)
Iexp <- electionDat$dexp*(electionDat$inc==1)+electionDat$rexp*(electionDat$inc==0)
#Iexp <- round(Iexp/(exp(1-slc1/3)*sd(Iexp,na.rm=T)))
Iexp <- quintile(Iexp,slices=slc1)
#Cexp <- round(Cexp/(exp(1-slc2/3)*sd(Cexp,na.rm=T)))
Cexp <- quintile(Cexp, slices=slc2)
Cvs <- electionDat$dv*(electionDat$inc==0)+(100-electionDat$dv)*(electionDat$inc==1)
Ivs <- electionDat$dvp*(electionDat$inc==1)+(100-electionDat$dvp)*(electionDat$inc==0)
#Ivs <- round(Ivs/(exp(1-slc3/3)*sd(Ivs,na.rm=T)))
Ivs <- quintile(Ivs, slices=slc3)
dat <- na.omit(data.frame(CNtriv,Cvs,Cwin,Cexp,Iexp,Cqual = as.numeric(electionDat$po1==1),year=electionDat$year,Rinc=as.numeric(electionDat$inc==0),Dinc = as.numeric(electionDat$inc==1),Ivs,yrStDist=electionDat$yrStDist))

ATTwin <- Match(dat$Cwin,Tr=dat$CNtriv,X=dat[,matchVecEx],exact=T,estimand="ATT")
ATEwin <- Match(dat$Cwin,Tr=dat$CNtriv,X=dat[,matchVecEx],exact=T,estimand="ATE")
ATCwin <- Match(dat$Cwin,Tr=dat$CNtriv,X=dat[,matchVecEx],exact=T,estimand="ATC")

### Checking Balance

CNtriv <- electionDat$DBClust*(electionDat$inc==0)+electionDat$RBClust*(electionDat$inc==1)
Cwin = (electionDat$pwin==1)*(electionDat$inc==0)+(electionDat$pwin==0)*(electionDat$inc==1)
Cexp <- electionDat$dexp*(electionDat$inc==0)+electionDat$rexp*(electionDat$inc==1)
Iexp <- electionDat$dexp*(electionDat$inc==1)+electionDat$rexp*(electionDat$inc==0)
#Iexp <- round(Iexp/(.5*sd(Iexp,na.rm=T)))
#Iexp <- quintile(Iexp)
#Cexp <- round(Cexp/(.5*sd(Cexp,na.rm=T)))
#Cexp <- quintile(Cexp)
Cvs <- electionDat$dv*(electionDat$inc==0)+(100-electionDat$dv)*(electionDat$inc==1)
Ivs <- electionDat$dvp*(electionDat$inc==1)+(100-electionDat$dvp)*(electionDat$inc==0)
#Ivs <- round(Ivs/(.5*sd(Ivs,na.rm=T)))
#Ivs <- quintile(Ivs)
dat <- na.omit(data.frame(CNtriv,Cvs,Cwin,Cexp,Iexp,Cqual = as.numeric(electionDat$po1==1),year=electionDat$year,Rinc=as.numeric(electionDat$inc==0),Dinc = as.numeric(electionDat$inc==1),Ivs))

balchk <- MatchBalance(matchForm,match.out=ATTwin,data=dat)

smallest <- balchk$AMsmallest.p.value

	
}



}

ATT <- ATTwin

###### ATC #######
smallest <- 0
slc1 <- 5
slc2 <- 6
slc3 <- 6
while(smallest < .1){

slc1 = slc1 + 2

library(Matching)
CNtriv <- electionDat$DBClust*(electionDat$inc==0)+electionDat$RBClust*(electionDat$inc==1)
Cwin = (electionDat$pwin==1)*(electionDat$inc==0)+(electionDat$pwin==0)*(electionDat$inc==1)
Cexp <- electionDat$dexp*(electionDat$inc==0)+electionDat$rexp*(electionDat$inc==1)
Iexp <- electionDat$dexp*(electionDat$inc==1)+electionDat$rexp*(electionDat$inc==0)
#Iexp <- round(Iexp/(exp(1-slc1/3)*sd(Iexp,na.rm=T)))
Iexp <- quintile(Iexp,slices=slc1)
#Cexp <- round(Cexp/(exp(1-slc2/3)*sd(Cexp,na.rm=T)))
Cexp <- quintile(Cexp, slices=slc2)
Cvs <- electionDat$dv*(electionDat$inc==0)+(100-electionDat$dv)*(electionDat$inc==1)
Ivs <- electionDat$dvp*(electionDat$inc==1)+(100-electionDat$dvp)*(electionDat$inc==0)
#Ivs <- round(Ivs/(exp(1-slc3/3)*sd(Ivs,na.rm=T)))
Ivs <- quintile(Ivs, slices=slc3)
dat <- na.omit(data.frame(CNtriv,Cvs,Cwin,Cexp,Iexp,Cqual = as.numeric(electionDat$po1==1),year=electionDat$year,Rinc=as.numeric(electionDat$inc==0),Dinc = as.numeric(electionDat$inc==1),Ivs,yrStDist=electionDat$yrStDist))

ATTwin <- Match(dat$Cwin,Tr=dat$CNtriv,X=dat[,matchVecEx],exact=T,estimand="ATT")
ATEwin <- Match(dat$Cwin,Tr=dat$CNtriv,X=dat[,matchVecEx],exact=T,estimand="ATE")
ATCwin <- Match(dat$Cwin,Tr=dat$CNtriv,X=dat[,matchVecEx],exact=T,estimand="ATC")

### Checking Balance

CNtriv <- electionDat$DBClust*(electionDat$inc==0)+electionDat$RBClust*(electionDat$inc==1)
Cwin = (electionDat$pwin==1)*(electionDat$inc==0)+(electionDat$pwin==0)*(electionDat$inc==1)
Cexp <- electionDat$dexp*(electionDat$inc==0)+electionDat$rexp*(electionDat$inc==1)
Iexp <- electionDat$dexp*(electionDat$inc==1)+electionDat$rexp*(electionDat$inc==0)
#Iexp <- round(Iexp/(.5*sd(Iexp,na.rm=T)))
#Iexp <- quintile(Iexp)
#Cexp <- round(Cexp/(.5*sd(Cexp,na.rm=T)))
#Cexp <- quintile(Cexp)
Cvs <- electionDat$dv*(electionDat$inc==0)+(100-electionDat$dv)*(electionDat$inc==1)
Ivs <- electionDat$dvp*(electionDat$inc==1)+(100-electionDat$dvp)*(electionDat$inc==0)
#Ivs <- round(Ivs/(.5*sd(Ivs,na.rm=T)))
#Ivs <- quintile(Ivs)
dat <- na.omit(data.frame(CNtriv,Cvs,Cwin,Cexp,Iexp,Cqual = as.numeric(electionDat$po1==1),year=electionDat$year,Rinc=as.numeric(electionDat$inc==0),Dinc = as.numeric(electionDat$inc==1),Ivs))

balchk <- MatchBalance(matchForm,match.out=ATCwin,data=dat)

smallest <- balchk$AMsmallest.p.value
 

if(smallest < .1){

slc2 <- slc2 +2


library(Matching)
CNtriv <- electionDat$DBClust*(electionDat$inc==0)+electionDat$RBClust*(electionDat$inc==1)
Cwin = (electionDat$pwin==1)*(electionDat$inc==0)+(electionDat$pwin==0)*(electionDat$inc==1)
Cexp <- electionDat$dexp*(electionDat$inc==0)+electionDat$rexp*(electionDat$inc==1)
Iexp <- electionDat$dexp*(electionDat$inc==1)+electionDat$rexp*(electionDat$inc==0)
#Iexp <- round(Iexp/(exp(1-slc1/3)*sd(Iexp,na.rm=T)))
Iexp <- quintile(Iexp,slices=slc1)
#Cexp <- round(Cexp/(exp(1-slc2/3)*sd(Cexp,na.rm=T)))
Cexp <- quintile(Cexp, slices=slc2)
Cvs <- electionDat$dv*(electionDat$inc==0)+(100-electionDat$dv)*(electionDat$inc==1)
Ivs <- electionDat$dvp*(electionDat$inc==1)+(100-electionDat$dvp)*(electionDat$inc==0)
#Ivs <- round(Ivs/(exp(1-slc3/3)*sd(Ivs,na.rm=T)))
Ivs <- quintile(Ivs, slices=slc3)
dat <- na.omit(data.frame(CNtriv,Cvs,Cwin,Cexp,Iexp,Cqual = as.numeric(electionDat$po1==1),year=electionDat$year,Rinc=as.numeric(electionDat$inc==0),Dinc = as.numeric(electionDat$inc==1),Ivs,yrStDist=electionDat$yrStDist))

ATTwin <- Match(dat$Cwin,Tr=dat$CNtriv,X=dat[,matchVecEx],exact=T,estimand="ATT")
ATEwin <- Match(dat$Cwin,Tr=dat$CNtriv,X=dat[,matchVecEx],exact=T,estimand="ATE")
ATCwin <- Match(dat$Cwin,Tr=dat$CNtriv,X=dat[,matchVecEx],exact=T,estimand="ATC")

### Checking Balance

CNtriv <- electionDat$DBClust*(electionDat$inc==0)+electionDat$RBClust*(electionDat$inc==1)
Cwin = (electionDat$pwin==1)*(electionDat$inc==0)+(electionDat$pwin==0)*(electionDat$inc==1)
Cexp <- electionDat$dexp*(electionDat$inc==0)+electionDat$rexp*(electionDat$inc==1)
Iexp <- electionDat$dexp*(electionDat$inc==1)+electionDat$rexp*(electionDat$inc==0)
#Iexp <- round(Iexp/(.5*sd(Iexp,na.rm=T)))
#Iexp <- quintile(Iexp)
#Cexp <- round(Cexp/(.5*sd(Cexp,na.rm=T)))
#Cexp <- quintile(Cexp)
Cvs <- electionDat$dv*(electionDat$inc==0)+(100-electionDat$dv)*(electionDat$inc==1)
Ivs <- electionDat$dvp*(electionDat$inc==1)+(100-electionDat$dvp)*(electionDat$inc==0)
#Ivs <- round(Ivs/(.5*sd(Ivs,na.rm=T)))
#Ivs <- quintile(Ivs)
dat <- na.omit(data.frame(CNtriv,Cvs,Cwin,Cexp,Iexp,Cqual = as.numeric(electionDat$po1==1),year=electionDat$year,Rinc=as.numeric(electionDat$inc==0),Dinc = as.numeric(electionDat$inc==1),Ivs))

balchk <- MatchBalance(matchForm,match.out=ATCwin,data=dat)

smallest <- balchk$AMsmallest.p.value

	
}

if(smallest < .1){

slc3 <- slc3 +2


library(Matching)
CNtriv <- electionDat$DBClust*(electionDat$inc==0)+electionDat$RBClust*(electionDat$inc==1)
Cwin = (electionDat$pwin==1)*(electionDat$inc==0)+(electionDat$pwin==0)*(electionDat$inc==1)
Cexp <- electionDat$dexp*(electionDat$inc==0)+electionDat$rexp*(electionDat$inc==1)
Iexp <- electionDat$dexp*(electionDat$inc==1)+electionDat$rexp*(electionDat$inc==0)
#Iexp <- round(Iexp/(exp(1-slc1/3)*sd(Iexp,na.rm=T)))
Iexp <- quintile(Iexp,slices=slc1)
#Cexp <- round(Cexp/(exp(1-slc2/3)*sd(Cexp,na.rm=T)))
Cexp <- quintile(Cexp, slices=slc2)
Cvs <- electionDat$dv*(electionDat$inc==0)+(100-electionDat$dv)*(electionDat$inc==1)
Ivs <- electionDat$dvp*(electionDat$inc==1)+(100-electionDat$dvp)*(electionDat$inc==0)
#Ivs <- round(Ivs/(exp(1-slc3/3)*sd(Ivs,na.rm=T)))
Ivs <- quintile(Ivs, slices=slc3)
dat <- na.omit(data.frame(CNtriv,Cvs,Cwin,Cexp,Iexp,Cqual = as.numeric(electionDat$po1==1),year=electionDat$year,Rinc=as.numeric(electionDat$inc==0),Dinc = as.numeric(electionDat$inc==1),Ivs,yrStDist=electionDat$yrStDist))

ATTwin <- Match(dat$Cwin,Tr=dat$CNtriv,X=dat[,matchVecEx],exact=T,estimand="ATT")
ATEwin <- Match(dat$Cwin,Tr=dat$CNtriv,X=dat[,matchVecEx],exact=T,estimand="ATE")
ATCwin <- Match(dat$Cwin,Tr=dat$CNtriv,X=dat[,matchVecEx],exact=T,estimand="ATC")

### Checking Balance

CNtriv <- electionDat$DBClust*(electionDat$inc==0)+electionDat$RBClust*(electionDat$inc==1)
Cwin = (electionDat$pwin==1)*(electionDat$inc==0)+(electionDat$pwin==0)*(electionDat$inc==1)
Cexp <- electionDat$dexp*(electionDat$inc==0)+electionDat$rexp*(electionDat$inc==1)
Iexp <- electionDat$dexp*(electionDat$inc==1)+electionDat$rexp*(electionDat$inc==0)
#Iexp <- round(Iexp/(.5*sd(Iexp,na.rm=T)))
#Iexp <- quintile(Iexp)
#Cexp <- round(Cexp/(.5*sd(Cexp,na.rm=T)))
#Cexp <- quintile(Cexp)
Cvs <- electionDat$dv*(electionDat$inc==0)+(100-electionDat$dv)*(electionDat$inc==1)
Ivs <- electionDat$dvp*(electionDat$inc==1)+(100-electionDat$dvp)*(electionDat$inc==0)
#Ivs <- round(Ivs/(.5*sd(Ivs,na.rm=T)))
#Ivs <- quintile(Ivs)
dat <- na.omit(data.frame(CNtriv,Cvs,Cwin,Cexp,Iexp,Cqual = as.numeric(electionDat$po1==1),year=electionDat$year,Rinc=as.numeric(electionDat$inc==0),Dinc = as.numeric(electionDat$inc==1),Ivs,yrStDist=as.character(electionDat$yrStDist)),stringsAsFactors=F)

balchk <- MatchBalance(matchForm,match.out=ATCwin,data=dat)

smallest <- balchk$AMsmallest.p.value

	
}



}

ATC <- ATCwin

### Save Results ###

treated <- as.character(dat$yrStDist[ATE$index.treated])
control <- as.character(dat$yrStDist[ATE$index.control])

resultDat <- data.frame(treated,control,stringsAsFactors=F)

write.csv(resultDat,"/users/brucedesmarais/Dropbox/professional/Research/Submitted/Camp/Tex/matchedObs.csv",row.names=F)




####### Create Balance Table ########

ATTtab <- matrix(0,4,3)
ATEtab <- matrix(0,4,3)
ATCtab <- matrix(0,4,3)

balATT <- MatchBalance(matchForm,match.out=ATT,data=dat)
balATE <- MatchBalance(matchForm,match.out=ATE,data=dat)
balATC <- MatchBalance(matchForm,match.out=ATC,data=dat)

rownames(ATTtab) <- rownames(ATEtab) <- rownames(ATCtab) <- c("Mean (treated)","Mean (control)","KS test","t-test")

ssize <- c(ssize,ATE$wnobs)

####### Barplot depicting estimates ########
library(gplots)
filet <- paste("/users/brucedesmarais/Dropbox/professional/Research/Submitted/Camp/Tex/","effects",matchVec[ex],".pdf",sep="")
pdf(filet,width=6,heigh=4,family="Times",pointsize=16)
par(las=1,mar=c(2,4,1,1))
library(gplots)
bars <- c(ATT$est,ATE$est,ATC$est)
se <- c(ATT$se,ATE$se,ATC$se)
cil= bars-1.96*se
ciu= bars+1.96*se
barplot2(bars,width=.5,plot.ci=T,ci.l=cil,ci.u=ciu,plot.grid=T,ylab="Effect on Pr(win)",grid.col="grey50",grid.lwd=1.25,names.arg=c("ATT","ATE","ATC"),space=.7)
dev.off()

print(ex)

EffectsList[[ex]] <- list(ATE=ATE,ATT=ATT,ATC=ATC)

}

lapply(EffectsList[[1]],summary)

